import {ref} from 'vue'
import {TodoItem} from '../type.d.ts'

let count = 0

export default function useTodoArr() {

  const todoArr = ref<Array<TodoItem>>([])

  // 增加项目
  const addTodoItem = (e: string) => {
    let res = {
      checked: false,
      id: count++,
      title: e
    }
    todoArr.value = [...todoArr.value, res]
  }
  // 切换任务的状态 
  const toggle = (e: number) => {
    
    let res = todoArr.value.map(item => {
      if (item.id === e) item.checked = !item.checked
      return item
    })

    todoArr.value = res
  }

  return {
    todoArr,
    toggle,
    addTodoItem
  }

} 
